package servlet;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
    private static final String DOWNLOAD_DIR = "D:\\JavaWeb\\WebDemo4\\download\\";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取下载文件的名字
        String name = req.getParameter("name");
        File file = new File(DOWNLOAD_DIR + name);
        if(file.exists()){
            byte[] data = name.getBytes(StandardCharsets.UTF_8);//获取下载的文件名的字节数据
            //转换编码格式，重新构建字符串，因为浏览器默认支持的编码是ISO_8859_1,因此，要转换为这种编码下中文才能正常显示
            name = new String(data, StandardCharsets.ISO_8859_1);
            //设置内容处理方案：以附件的形式处理
            resp.setHeader("Content-Disposition", "attachment; filename=" + name);
            InputStream in = new FileInputStream(file);
            OutputStream out = resp.getOutputStream();
            IOUtils.copy(in, out);//传输信息
            out.flush();
            in.close();
        }else {
            resp.setCharacterEncoding("UTF-8");
            resp.setContentType("text/html;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.print("下载的文件不存在");
            writer.flush();
            writer.close();
        }
    }
}
